/**
 *  Copyright (C) 2006 zhangbo (freeeob@gmail.com)
 *
 *  This product is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1 of the License, or
 *  (at your option) any later version.
 * 
 *  This product is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 *
 *  author:zhangbo
 *  Email:jsjava@gmail.com
 */

/**
 * constructor
 * @param newOrientation
 * @param leftPane
 * @param rightPane
 */ 
function HTMLSplitPane(newOrientation,leftPaneId,rightPaneId,midPaneId){	
	this.jsjava_class="jsorg.eob.component.splitor.HTMLSplitPane";
	if(!StringUtils.isNumeric(newOrientation)||newOrientation!=0&&newOrientation!=1){
		throw new IllegalArgumentException(IllegalArgumentException.ERROR,"The split span's orientation must be 0 or 1!");
	}
	this.newOrientation=newOrientation;
	this.leftPaneId=leftPaneId;
	this.midPaneId=midPaneId;
	this.isResizing=false;
	var uuid=RandomStringUtils.randomAlphabetic(5);
	var leftPane=document.getElementById(leftPaneId);
	var rightPane=document.getElementById(rightPaneId);
	var midPane=document.getElementById(midPaneId);
	if(newOrientation==0){
		midPane.style.cursor="col-resize";
		EventUtils.addDomEvent(midPane,"mousedown",new Function("var instance=HTMLSplitPane.instances['"+uuid+"'];var formatEvent = EventUtils.getEvent();origX=formatEvent.pageX;instance.isResizing=true;var obj = document.getElementById('jsjava_float_split_bar');obj.style.display='inline';"),false);
		EventUtils.addDomEvent(document,"mousemove",new Function("var instance=HTMLSplitPane.instances['"+uuid+"'];if(instance.isResizing){var formatEvent = EventUtils.getEvent();var resizeX = formatEvent.pageX;var obj = document.getElementById('jsjava_float_split_bar');var sObj= document.getElementById('"+midPaneId+"');obj.innerHTML=sObj.innerHTML;var objRect=DocumentUtils.getElementRectangle(sObj);obj.style.top=objRect.getY()+document.body.scrollTop;obj.style.left=resizeX-objRect.getWidth()/2;obj.style.height=objRect.getHeight();}"),false);
		EventUtils.addDomEvent(document,"mouseup",new Function("var instance=HTMLSplitPane.instances['"+uuid+"'];if(instance.isResizing){DocumentUtils.cancelSelection();var obj = document.getElementById('jsjava_float_split_bar');obj.style.display='none';var uObj = document.getElementById('"+leftPaneId+"');var uObjRect=DocumentUtils.getElementRectangle(uObj);var uObjBottomX=uObjRect.getX()+document.body.scrollLeft;var formatEvent = EventUtils.getEvent();var floatBarX=formatEvent.pageX;var addWidth=floatBarX-origX;var dObj = document.getElementById('"+rightPaneId+"');var dObjRect=DocumentUtils.getElementRectangle(dObj);try{DocumentUtils.setDomWidth(uObj,uObjRect.getWidth()+addWidth);DocumentUtils.setDomWidth(dObj,dObjRect.getWidth()-addWidth);}catch(e){}var sObj= document.getElementById('"+midPaneId+"');EventUtils.releaseCapture(sObj);}instance.isResizing=false;"),false);		
	}else{
		midPane.style.cursor="row-resize";
		EventUtils.addDomEvent(midPane,"mousedown",new Function("var instance=HTMLSplitPane.instances['"+uuid+"'];var formatEvent = EventUtils.getEvent();instance.isResizing=true;var obj = document.getElementById('jsjava_float_split_bar');obj.style.display='inline';"),false);
		EventUtils.addDomEvent(document,"mousemove",new Function("var instance=HTMLSplitPane.instances['"+uuid+"'];if(instance.isResizing){var formatEvent = EventUtils.getEvent();var resizeY = formatEvent.pageY;var obj = document.getElementById('jsjava_float_split_bar');var sObj= document.getElementById('"+midPaneId+"');obj.innerHTML=sObj.innerHTML;var objRect=DocumentUtils.getElementRectangle(sObj);obj.style.left=objRect.getX();obj.style.top=resizeY-objRect.getHeight()/2;obj.style.width=objRect.getWidth();}"),false);
		EventUtils.addDomEvent(document,"mouseup",new Function("var instance=HTMLSplitPane.instances['"+uuid+"'];if(instance.isResizing){DocumentUtils.cancelSelection();var obj = document.getElementById('jsjava_float_split_bar');obj.style.display='none';var uObj = document.getElementById('"+leftPaneId+"');var uObjRect=DocumentUtils.getElementRectangle(uObj);var uObjBottomY=uObjRect.getY()+uObjRect.getHeight()+document.body.scrollTop;var formatEvent = EventUtils.getEvent();var floatBarY=formatEvent.pageY;var addHeight=floatBarY-uObjBottomY;var dObj = document.getElementById('"+rightPaneId+"');var dObjRect=DocumentUtils.getElementRectangle(dObj);try{DocumentUtils.setDomHeight(uObj,uObjRect.getHeight()+addHeight);DocumentUtils.setDomHeight(dObj,dObjRect.getHeight()-addHeight);}catch(e){}var sObj= document.getElementById('"+midPaneId+"');EventUtils.releaseCapture(sObj);}instance.isResizing=false;"),false);
	}
	HTMLSplitPane.instances[uuid]=this;
}

HTMLSplitPane.HORIZONTAL_SPLIT = 0;
HTMLSplitPane.VERTICAL_SPLIT = 1;

HTMLSplitPane.instances=new Array();

/**
 * show the splitpane
 */ 
HTMLSplitPane.prototype.show=function(){
	if(!document.getElementById("jsjava_float_split_bar")){
		var elem=null;
		if(BrowserUtils.isIE()){
			elem=document.createElement("<div id='jsjava_float_split_bar' style='position:absolute;left:3000;top:3000;display:none;'>&nbsp;</div>");
		}else{
			elem=document.createElement("div");
			elem.setAttribute("id","jsjava_float_split_bar");
	    	elem.setAttribute("style","position:absolute;left:3000;top:3000;display:none;");
		}
		document.body.appendChild(elem);
	}
};